﻿using AutoMapper;
using CSRedis;
using SH.Common;
using SH.Common.CommonReturn;
using SH.Common.DataDictionary;
using SH.Customer.Application.IApplication.ICustomer;
using SH.Customer.Application.IApplication.IEquities;
using SH.Customer.Domain.CustomerClass;
using SH.Customer.ErrorCodeEnum;
using SH.Customer.Infrastructure.IRepository;
using SH.Customer.Infrastructure.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace SH.Customer.Application.Application
{
    public class EquitiesApplication : BaseApplication<Equities>, IEquitiesApplication
    {
        private readonly IEquitiesRepository _equitiesRepository;

        private readonly IMapper _mapper;

        private readonly CSRedisClient _redis = new CSRedisClient(AppService.Configuration["redisIP"]);

        public EquitiesApplication(IEquitiesRepository equitiesRepository, IMapper mapper) : base(equitiesRepository)
        {
            _equitiesRepository = equitiesRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 删除权益信息
        /// </summary>
        /// <param name="eId"></param>
        /// <returns></returns>
        public ApiResult<int> DeleteEquities(int eId)
        {
            ApiResult<int> apiResult = new ApiResult<int>();

            var num = _equitiesRepository.DeleteEquities(eId);

            apiResult.Success();

            return apiResult;
        }

        /// <summary>
        /// 添加权益信息
        /// </summary>
        /// <param name="equitiesDto"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> EquitiesAdd(EquitiesDto equitiesDto)
        {
            ApiResult<int> apiResult = new ApiResult<int>();

            var dto = _mapper.Map<Equities>(equitiesDto);

            dto.AddTime = DateTime.Now;

            var eName = await _equitiesRepository.GetModelByConditionAsync(m => m.EName == dto.EName);
            if (eName != null)
            {
                apiResult.Error(ApiResult<int>.ErrorType.Request, "已有相同名称的权益");
                return apiResult;
            }

            await _equitiesRepository.InsertAsync(dto);

            apiResult.Success();

            return apiResult;
        }

        /// <summary>
        /// 获取权益详情
        /// </summary>
        /// <param name="eId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Equities> GetEquitiesDelitalAsync(int eId)
        {
            var list = await _equitiesRepository.GetModelByConditionAsync(m => m.EId == eId);
            return list;
        }

        /// <summary>
        /// 获取权益信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<Equities>> GetEquitiesListAsync()
        {
            // 从数据库中获取数据列表  
            var result = _redis.Get<List<Equities>>("Equities");

            if (result == null || !result.Any())
            {
                var list = (await _equitiesRepository.GetListAsync()).OrderByDescending(x => x.AddTime).ToList();

                var random = new Random();

                var time = random.Next(3, 5);

                _redis.Set("Equities", list, TimeSpan.FromSeconds(time));

                result = list;
            }

            return result;
        }

        /// <summary>
        /// 更新权益信息
        /// </summary>
        /// <param name="updDto"></param>
        /// <returns></returns>
        public ApiResult<int> UpdEquities(EquitesUpdDto updDto)
        {
            ApiResult<int> apiResult = new ApiResult<int>();

            var item = _mapper.Map<Equities>(updDto);

            item.AddTime = DateTime.Now;

            var info = _equitiesRepository.Upd(item);

            apiResult.Success(info);

            return apiResult;
        }
    }
}
